package com.jin.lazytool.customevent;

import com.jin.lazytool.componment.NumberField;
import com.jin.lazytool.componment.datetime.DateTimePicker;
import com.jin.lazytool.entity.IEvent;
import com.jin.lazytool.entity.LoopStartEvent;
import com.jin.lazytool.handler.LoopType;
import com.jin.lazytool.rewritte.ChoiceBoxSelectModel;
import com.jin.lazytool.utils.JinUtils;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.GridPane;
import javafx.util.StringConverter;

import java.time.LocalDateTime;

public class CustomLoopStartEvent extends CustomEvent{

    private final TextField loopNum = new TextField();
    private final ChoiceBox<LoopType> loopType = new ChoiceBox<>();
    private final NumberField count = new NumberField();
    private final NumberField timer = new NumberField();
    private final DateTimePicker dateTime = new DateTimePicker();
    private LoopStartEvent event;

    {
        timer.setPromptText("单位/秒");
        loopType.setConverter(new StringConverter<LoopType>() {
            @Override
            public String toString(LoopType loopType) {
                return loopType == null ? "" : loopType.getDesc();
            }

            @Override
            public LoopType fromString(String s) {
                return LoopType.convert(s);
            }
        });
        loopType.setItems(LoopType.loopTypes);

    }

    @Override
    public void initAddPanel(GridPane grid) {
        loopNum.setEditable(true);
        setLoopTypeSelectionModel(grid);
        loopType.setValue(LoopType.loopTypes.get(0));
    }

    @Override
    public void initEditPanel(GridPane grid,IEvent event) {
        this.event = (LoopStartEvent) event;
        loopNum.setText(this.event.getLoopNum());
        loopNum.setEditable(false);
        time.setValue(event.getTime());
        count.setValue(this.event.getLoopCount());
        timer.setValue(this.event.getTimer());
        luaScript.setText(this.event.getLua());
        dateTime.setTimeProperty(this.event.getDateTime());
        setLoopTypeSelectionModel(grid);
        loopType.setValue(this.event.getType());
    }

    @Override
    public IEvent fullAddIEvent() {
        event = new LoopStartEvent();
        IEvent[] loopEventList = CustomEvent.LOOP_EVENTS.get(loopNum.getText());
        if(loopEventList != null && loopEventList[0] != null){
            JinUtils.error(loopNum.getText() + "循环编号已经存在");
        }
        checkAndSetValue();
        return event;
    }

    @Override
    public void fullEditIEvent() {
        checkAndSetValue();
    }

    @Override
    public Class<? extends IEvent> eventClass() {
        return LoopStartEvent.class;
    }

    private void checkAndSetValue() {
        if(loopNum.getText() == null || "".equals(loopNum.getText())){
            JinUtils.error("循环编号不能为空");
        }

        event.setType(loopType.getValue());
        event.setLoopNum(loopNum.getText());
        event.setTime(time.getNullDefaultValue());
        Tooltip.install(luaButton, new Tooltip("""
                返回对象
                {
                    exeFlag: true, // 是否执行        必须返回
                    time: 0        // 执行前等待时间  必须返回
                    ...
                }"""));
        switch (loopType.getValue()) {
            case COUNT -> {
                if (count.getValue() == null) {
                    JinUtils.error("计数不能为空");
                }
                event.setLoopCount(count.getValue());
            }
            case TIMER -> {
                if (timer.getValue() == null) {
                    JinUtils.error("计时不能为空");
                }
                event.setTimer(timer.getNullDefaultValue());
            }
            case LUA -> {
                Tooltip.install(luaButton, new Tooltip("""
                返回对象
                {
                    exeFlag: true, // 是否执行        必须返回
                    time: 0        // 执行前等待时间    必须返回
                }"""));
                if (luaScript.getText() == null || "".equals(luaScript.getText().trim())) {
                    JinUtils.error("while脚本不能为空");
                }
                event.setLua(luaScript.getText());
            }
            case TIME -> {
                if (dateTime.dateTimeProperty() == null || dateTime.dateTimeProperty().getValue() == null) {
                    JinUtils.error("截止时间不能为空");
                }
                event.setDateTime(dateTime.dateTimeProperty().getValue());
            }
        }
    }


    @Override
    public String getName() {
        return "循环开始";
    }

    private final Label nameLabel = new Label();
    private void setLoopTypeSelectionModel(GridPane grid) {

        loopType.setSelectionModel(new ChoiceBoxSelectModel<>(loopType, () -> {}, param -> {
            grid.getChildren().removeAll(nameLabel,count,timer, luaScript,dateTime, luaButton);
            nameLabel.setText(param.getDesc());
            switch (param) {
                case COUNT -> {
                    grid.addRow(5,nameLabel,count);
                }
                case TIMER -> {
                    grid.addRow(5,nameLabel,timer);
                }
                case LUA -> {
                    grid.addRow(5, luaButton, luaScript);
                }
                case TIME -> {
                    dateTime.setTimeProperty(LocalDateTime.now());
                    grid.addRow(5,nameLabel,dateTime);
                }
            }
        }));
        grid.addRow(2,new Label("循环类型"),loopType);
        grid.addRow(3,new Label("循环编号"),loopNum);
        grid.addRow(4,new Label("循环前等待时间"),time);
    }


}
